package com.shixulin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shixulin.domain.ResponseResult;
import com.shixulin.entity.Course;
import com.shixulin.entity.DTO.QuestionQueryDTO;
import com.shixulin.entity.QuestionBank;
import com.shixulin.mapper.QuestionBankMapper;
import com.shixulin.service.QuestionBankService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.List;

/**
 * (QuestionBank)表服务实现类
 *
 * @author makejava
 * @since 2023-11-18 19:08:08
 */
@Service("questionBankService")
public class QuestionBankServiceImpl extends ServiceImpl<QuestionBankMapper, QuestionBank> implements QuestionBankService {
    @Autowired
    private QuestionBankMapper questionBankMapper;

    @Override
    public ResponseResult uploadQuestion(QuestionBank questionBank) {
        try{
            int insert = this.baseMapper.insert(questionBank);
            if(insert>0){
                return new ResponseResult(300,"上传成功");
            }else {
                return new ResponseResult(506,"上传失败");
            }
        }catch (Exception e) {
            e.printStackTrace();
            return new ResponseResult(404,"错误");
        }

    }

    @Override
    public ResponseResult getQuestionBankByPage(QuestionQueryDTO questionQueryDTO) {
        Page<QuestionBank> rowPage = new Page(questionQueryDTO.getCurrent(), questionQueryDTO.getSize());
        try{
            LambdaQueryWrapper<QuestionBank> queryWrapper = new LambdaQueryWrapper<>();
            rowPage = this.baseMapper.selectPage(rowPage, queryWrapper);
            if (rowPage != null) {
                return new ResponseResult(300,"查询成功",rowPage);
            }else {
                return new ResponseResult(506,"查询失败");
            }

        }catch (Exception e) {
            e.printStackTrace();
            return new ResponseResult(506,"错误");
        }
        //queryWrapper组装查询where条件
    }

    @Override
    public ResponseResult getQuestionsById(int id) {
        return new ResponseResult<>(300,"查询成功",this.baseMapper.selectById(id));
    }

    @Override
    public ResponseResult editQuestion(QuestionBank question) {

        try{
            int update = this.baseMapper.updateById(question);
            if (update > 0) {
                return new ResponseResult(300,"修改成功");
            }else {
                return new ResponseResult(505,"修改失败");
            }
        } catch (Exception e) {
            return new ResponseResult(405,"修改失败");
        }
    }

    @Override
    public ResponseResult deleteQuestionById(int id) {
        int delete = this.baseMapper.deleteById(id);
        try {
           if (delete > 0) {
               return new ResponseResult(300,"删除成功");
           }else {
               return new ResponseResult(506,"删除失败");
           }
       }catch (Exception e) {
            return new ResponseResult(406,"错误");
       }

    }

    @Override
    public ResponseResult selectSubjectQuestion(int subjectId) {
        try{
           List<QuestionBank> questions = questionBankMapper.selectSubjectQuestion(subjectId);
           if (questions != null && questions.size() > 0){
               return new ResponseResult(300,"查询成功",questions);
           }else {
               return new ResponseResult(505,"查询失败");
           }
        }catch (Exception e) {
            return new ResponseResult(405,"错误");
        }

    }

}
